﻿namespace ImageProcessing
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Windows.Forms.DataVisualization.Charting;

    /// <summary>
    /// Histogram Form
    /// </summary>
    public partial class HistogramChart : Form
    {
        /// <summary>
        /// Object of class Histogram
        /// </summary>
        private Histogram histo = new Histogram();

        /// <summary>
        /// Checks if its busy.
        /// </summary>
        private bool busy = false;

        /// <summary>
        /// Histogram chart
        /// </summary>
        private Chart histochart;

        /// <summary>
        /// Checkboxes for each color to appear
        /// </summary>
        private CheckBox checkRed, checkGreen, checkBlue, checkGray;

        /// <summary>
        /// Reference to the image
        /// </summary>
        private DImage image;

        /// <summary>
        /// Initializes a new instance of the HistogramChart class.
        /// </summary>
        /// <param name="i">The image reference.</param>
        public HistogramChart(DImage i)
        {
            this.InitializeComponent();
            this.image = i;
            this.GetColorArrays();
            this.AddAllPoints();
            this.Chk_Red.Checked = true;
            this.Chk_Green.Checked = true;
            this.Chk_Blue.Checked = true;
            this.Chk_Gray.Checked = true;
        }

        /// <summary>
        /// Initializes a new instance of the HistogramChart class.
        /// </summary>
        public HistogramChart()
        {
            this.InitializeComponent();
            this.Chk_Red.Checked = true;
            this.Chk_Green.Checked = true;
            this.Chk_Blue.Checked = true;
            this.Chk_Gray.Checked = true;
            this.Chk_Red.Visible = false;
            this.Chk_Green.Visible = false;
            this.Chk_Blue.Visible = false;
            this.Chk_Gray.Visible = false;
            this.checkRed = this.Chk_Red;
            this.checkGreen = this.Chk_Green;
            this.checkBlue = this.Chk_Blue;
            this.checkGray = this.Chk_Gray;
            this.HistoChart = this.Chart_Histogram;
        }

        /// <summary>
        /// Gets or sets the check red.
        /// </summary>
        /// <value>
        /// The check red.
        /// </value>
        public CheckBox CheckRed
        {
            get { return this.checkRed; }
            set { this.checkRed = value; }
        }

        /// <summary>
        /// Gets or sets the check green.
        /// </summary>
        /// <value>
        /// The check green.
        /// </value>
        public CheckBox CheckGreen
        {
            get { return this.checkGreen; }
            set { this.checkGreen = value; }
        }

        /// <summary>
        /// Gets or sets the check blue.
        /// </summary>
        /// <value>
        /// The check blue.
        /// </value>
        public CheckBox CheckBlue
        {
            get { return this.checkBlue; }
            set { this.checkBlue = value; }
        }

        /// <summary>
        /// Gets or sets the check gray.
        /// </summary>
        /// <value>
        /// The check gray.
        /// </value>
        public CheckBox CheckGray
        {
            get { return this.checkGray; }
            set { this.checkGray = value; }
        }

        /// <summary>
        /// Gets or sets the histo chart.
        /// </summary>
        /// <value>
        /// The histo chart.
        /// </value>
        public Chart HistoChart
        {
            get { return this.histochart; }
            set { this.histochart = value; }
        }

        /// <summary>
        /// Gets the color arrays.
        /// </summary>
        public void GetColorArrays()
        {
            this.histo.UpdateHistogram(this.image);
        }

        /// <summary>
        /// Adds the red points to the Chart.
        /// </summary>
        public void AddRedPoints()
        {
            for (int i = 0; i < 256; i++)
            {
                this.Chart_Histogram.Series["Red"].Points.Add(new DataPoint(i, this.histo.Red1[i]));
            }
        }

        /// <summary>
        /// Adds the green points to the Chart.
        /// </summary>
        public void AddGreenPoints()
        {
            for (int i = 0; i < 256; i++)
            {
                this.Chart_Histogram.Series["Green"].Points.Add(new DataPoint(i, this.histo.Green1[i]));
            }
        }

        /// <summary>
        /// Adds the blue points to the Chart.
        /// </summary>
        public void AddBluePoints()
        {
            for (int i = 0; i < 256; i++)
            {
                this.Chart_Histogram.Series["Blue"].Points.Add(new DataPoint(i, this.histo.Blue1[i]));
            }
        }

        /// <summary>
        /// Adds the gray points to the Chart.
        /// </summary>
        public void AddGrayPoints()
        {
            for (int i = 0; i < 256; i++)
            {
                this.Chart_Histogram.Series["Gray"].Points.Add(new DataPoint(i, this.histo.Gray1[i]));
            }
        }

        /// <summary>
        /// Adds all points.
        /// </summary>
        public void AddAllPoints()
        {
            this.AddRedPoints();
            this.AddGreenPoints();
            this.AddBluePoints();
            this.AddGrayPoints();
        }

        /// <summary>
        /// Updatehistoes the specified i.
        /// </summary>
        /// <param name="i">The i.</param>
        public void Updatehisto(DImage i)
        {
            this.histo = new Histogram();
            this.Chk_Red.Visible = true;
            this.Chk_Green.Visible = true;
            this.Chk_Blue.Visible = true;
            this.Chk_Gray.Visible = true;
            this.Chart_Histogram.Series["Gray"].Points.Clear();
            this.Chart_Histogram.Series["Red"].Points.Clear();
            this.Chart_Histogram.Series["Green"].Points.Clear();
            this.Chart_Histogram.Series["Blue"].Points.Clear();
            this.image = i;
            this.GetColorArrays();
            this.AddAllPoints();
        }

        /// <summary>
        /// Handles the CheckedChanged event of the Chk control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Chk_CheckedChanged(object sender, EventArgs e)
        {
            if (this.busy)
            {
                return;
            }

            this.busy = true;

            CheckBox chk = (CheckBox)sender;
            if (ModifierKeys == Keys.Shift)
            {
                this.DisableAllSeries();
                chk.Checked = true;
                this.Chart_Histogram.Series[chk.Name.Substring(4)].Enabled = true;
            }
            else if (Control.ModifierKeys == Keys.ControlKey)
            {
                this.EnableAllSeries();
                chk.Checked = false;
                this.Chart_Histogram.Series[chk.Name.Substring(4)].Enabled = false;
            }
            else
            {
                if (chk.Checked)
                {
                    this.Chart_Histogram.Series[chk.Name.Substring(4)].Enabled = true;
                }
                else
                {
                    this.Chart_Histogram.Series[chk.Name.Substring(4)].Enabled = false;
                }
            }

            this.busy = false;
        }

        /// <summary>
        /// Disables all series.
        /// </summary>
        private void DisableAllSeries()
        {
            for (int i = 0; i < this.Chart_Histogram.Series.Count; i++)
            {
                this.Chart_Histogram.Series[i].Enabled = false;
            }

            this.Chk_Blue.Checked = this.Chk_Red.Checked = this.Chk_Green.Checked = this.Chk_Gray.Checked = false;
        }

        /// <summary>
        /// Enables all series.
        /// </summary>
        private void EnableAllSeries()
        {
            for (int i = 0; i < this.Chart_Histogram.Series.Count; i++)
            {
                this.Chart_Histogram.Series[i].Enabled = true;
            }

            this.Chk_Blue.Checked = this.Chk_Red.Checked = this.Chk_Green.Checked = this.Chk_Gray.Checked = true;
        }
    }
}
